package co.chuangdian.oflauncher.setting;

import org.json.JSONException;
import org.json.JSONObject;

import uc.PasswordType;
import uc.SubTitleView;
import uc.numberlock.PasswordActivity;
import uc.patternlock.UnlockGesturePasswordActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import cn.onekit.*;
import cn.onekit.android.*;
import co.chuangdian.oflauncher.OFActivity;
import co.chuangdian.oflauncher.R;



public class LockScreenActivity extends OFActivity implements OnClickListener {  
	
	CheckBox password_typeSwitch, lockscreenSwitch, smsPreviewSwitch,
			shakeSwitch, noticebarSwitch;
	JSONObject setting;
	JSONObject lockscreen;

	uc.TableRow comfirm_password;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_lockscreen);
		MESSAGE.receive("setting_password_type",null, new CALLBACK<Bundle>() {

			@Override
			public void run(boolean isError, Bundle result) {
				loadSetting();
			}

		});
		this.findViewById(R.id.setting_lockscreen_passwordrow)
		.setOnClickListener(this);
		
		//
		//
		lockscreenSwitch = (CheckBox) findViewById(R.id.lockScreenSwitch);
		lockscreenSwitch
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						try {
							lockscreen.put("switch", isChecked);
							setting.put("lockscreen", lockscreen);
							CONFIG.set("setting", setting);
							loadSetting();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
			
		//
		smsPreviewSwitch = (CheckBox) findViewById(R.id.smsPreviewSwitch);
		smsPreviewSwitch
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						try {
							lockscreen.put("message_preview", isChecked);
							setting.put("lockscreen", lockscreen);
							CONFIG.set("setting", setting);
							loadSetting();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
		//
		noticebarSwitch = (CheckBox) findViewById(R.id.noticebarSwitch);
		noticebarSwitch
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						try {
							lockscreen.put("statusbar_notice", isChecked);
							setting.put("lockscreen", lockscreen);
							CONFIG.set("setting", setting);
							loadSetting();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});

		//4-27
		comfirm_password = (uc.TableRow)findViewById(R.id.setting_lockscreen_passwordrow);

		loadSetting();
	}
	/**
	 * 
	 * */
	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.setting_lockscreen_passwordrow:
			confirmPassword();
			break;

		default:
			break;
		}
	}

	
	void loadSetting() {
		String settingstring = (String) CONFIG.get("setting");
		setting = JSON.parse(settingstring);
		lockscreen = setting.optJSONObject("lockscreen");

		boolean lockscreen_switch = lockscreen.optBoolean("switch");
		lockscreenSwitch.setChecked(lockscreen_switch);
		((SubTitleView)this.findViewById(R.id.lockScreenTextView)).setSubtitle(getString(lockscreen_switch?R.string.setting_lockscreen_switch_on:R.string.setting_lockscreen_switch_off));
	//

		boolean mess_preview = lockscreen.optBoolean("message_preview");
		smsPreviewSwitch.setChecked(mess_preview);	
		((SubTitleView)this.findViewById(R.id.smsPreviewTextView)).setSubtitle(getString(mess_preview?R.string.setting_lockscreen_messagepreview_on:R.string.setting_lockscreen_messagepreview_off));

		//

		boolean statusbar_notice = lockscreen.optBoolean("statusbar_notice");
		noticebarSwitch.setChecked(statusbar_notice);
		((SubTitleView)this.findViewById(R.id.noticebarTextView)).setSubtitle(getString(statusbar_notice?R.string.setting_lockscreen_noticebar_on:R.string.setting_lockscreen_noticebar_off));

		//
		SubTitleView passwordTypeTextView = (SubTitleView) this
				.findViewById(R.id.passwordTypeTextView);
		int password_type_int = lockscreen.optInt("password_type");
		PasswordType password_type = PasswordType.values()[password_type_int];
		String passwrodTypeString;
		switch (password_type) {
		case none:
			passwrodTypeString = this
					.getString(R.string.setting_lockscreen_password_none);
			break;
		case pattern:
			passwrodTypeString = this
					.getString(R.string.setting_lockscreen_password_pattern);
			break;
		case number:
			passwrodTypeString = this
					.getString(R.string.setting_lockscreen_password_number);
			break;
		default:
			passwrodTypeString = "";
			break;
		}
		passwordTypeTextView.setSubtitle(passwrodTypeString);
	}
	
	
	
	/*
	 * 确认是否存在密码
	 * */
	void confirmPassword(){
		loadSetting();
		int password_type_int = lockscreen.optInt("password_type");
		PasswordType password_type = PasswordType.values()[password_type_int];
 
			switch (password_type) {
			case none:
				startActivity(new Intent(this, LockScreen_PasswordActivity.class));
				break;
				
			case pattern:
				//4-28
				//startActivity(new Intent(this, CreateGesturePasswordActivity.class));
				startActivity(new Intent(this, UnlockGesturePasswordActivity.class));
				break;
				
			case number:
				startActivity(new Intent(this, PasswordActivity.class));
				break;
				
			default:
				break;
				}
			
	}

}

		
